package com.item.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.item.mapper.VideoMapper;
import com.item.pojo.Video;
import com.item.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/video")
public class VideoController {

    @Resource
    private VideoMapper videoMapper;


    @GetMapping
    public List<Video> selectAll(){
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        return videoMapper.selectList(queryWrapper);
    }

    @GetMapping("/getById")
    public Video selectById(@RequestParam Integer id){
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return videoMapper.selectOne(queryWrapper);
    }

    @GetMapping("/getByCollect")
    public  List<Video> selectByCollect(){
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_collect",true);
        return videoMapper.selectList(queryWrapper);
    }

    @GetMapping("/pageList")
    public Page<Video> pageList(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam String type){
        //操作数数据库对象
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("type",type);

        String sql="limit "+(pageNum-1)*pageSize+","+pageSize;

        Page<Video> videoPage =videoMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper);

        videoPage.setTotal(videoMapper.selectCount(queryWrapper));

        videoPage.setRecords(videoMapper.selectList(queryWrapper.last(sql)));

        return videoPage;
    }


    //新增和修改
    @PostMapping
    public Object save(@RequestBody Video video){
        if (video.getId() == null) {
            video.setCreateTime(new Date());
            System.out.println(video);
            return  videoMapper.insert(video);
        }
        return videoMapper.updateById(video);
    }


    @GetMapping("/{type}")
    public List<Video> selectBy( @PathVariable String type){
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("type",type);
        return videoMapper.selectList(queryWrapper);
    }


    @DeleteMapping("/{id}")
    public int delete(@PathVariable Integer id){
        int delete = videoMapper.deleteById(id);
        return delete;
    }

    //批量删除
    @PostMapping("/del/batch")
    public int delBatch(@RequestBody List<Integer> ids){
        return videoMapper.deleteBatchIds(ids);
    }




}
